<template>
  <el-dialog :title="title" :model-value="props.modelValue" width="950px"   @close="$emit('update:modelValue', false)"  :close-on-click-modal="false">

      <ElTableWithColumns :height="150" :data="dataList" :columns="columns" :operateColProps="{ width: 120, fixed: false }" :keyId="props.keyId"> </ElTableWithColumns>
       <template #footer>
         <div class="dialog-footer">
           <el-button type="primary" v-if="props.success"  @click="mdySubmit">确 定</el-button>
           <el-button @click="$emit('update:modelValue', false)">取 消</el-button>
         </div>
       </template>
  </el-dialog>
</template>

<script  setup lang="ts">
import { computed } from 'vue';

  const { proxy } = getCurrentInstance() as ComponentInternalInstance
  const sucList = ref([]);
  const emit = defineEmits(['update:modelValue','addBill'])

  const mdyLoading = ref(false)
  // const tableData = ref([])
  interface Props {
      /**
       * 导入结果
      */
      success: boolean;
      allRecords:number;
      /**
       * 是否导入
      */
      imported: boolean;
      list: any[];
      keyId:string;
  }


  const props = withDefaults(defineProps<Props>(), {
      success: false,
      imported: false,
      keyId:'',
      allRecords:0,
      list: () => []
  });

const title = computed(()=>{
    if(props.success){
        return "确认打款";
    }else{
        return "错误提示";
    }

})

const mdySubmit = ()=>{
  emit('addBill',{});
}


  const columns = computed(() => {
      if (props.success) {
          return [
              {
                  title: '发放金额',
                  dataIndex: 'sumMoneyDecimal',
              },
              {
                  title: '打款笔数',
                  dataIndex: 'billIds',
              },
              {
                  title: '平均金额（元）',
                  dataIndex: 'avgDec',
              },
              {
                  title: '最低金额（元）',
                  dataIndex: 'minDec',
              },
              {
                  title: '最高金额（元）',
                  dataIndex: 'maxDec',
              },
              {
                  title: '企业支付金额（元）',
                  dataIndex: 'qyzf',
                  renderText:function(row:any){
                    return proxy?.convertFenToYuan(row.qyzf)
                  }
              },
              {
                  title: '服务费（元）',
                  dataIndex: 'serviceChargeMoney',
                  renderText:function(row:any){
                    return proxy?.convertFenToYuan(row.serviceChargeMoney)
                  }
              },
              {
                  title: '自由职业者实得金额（元）',
                  dataIndex: 'amount',
                  renderText:function(row:any){
                    return proxy?.convertFenToYuan(row.amount)
                  }
              },
          ]
      }
      return  [
              {
                  title: '姓名',
                  dataIndex: 'workerName',
              },
              {
                  title: '身份证号',
                  dataIndex: 'cardId',
              },
              {
                  title: '发放金额（元）',
                  dataIndex: 'moneyDecimal',
              },
              {
                  title: '备注',
                  dataIndex: 'errorMsg',
                  renderText:function(row:any){
                    return "<span style='color:red;'>"+row.errorMsg+"</span>";
                  }
              },
          ]
  })


//获取提交成功的LIst
const getSuccessList = ()=>{

  let list = props.list;
  let item = {
      billIds: list.length
  }
  let max = 0;
  let min = 0;
  let sumMoneyDecimal:number = 0; //发放金额,元
  let sumQyzf = 0 ;//企业支付，分
  let sumFwf = 0 ;//服务费,分
  let sumSd = 0 ;//实际得到，分

  list.map((it,idx)=>{
      if(idx == 0){
        max = parseFloat(it.moneyDecimal);
        min = parseFloat(it.moneyDecimal);
      }
      sumMoneyDecimal = parseFloat(sumMoneyDecimal)+ parseFloat(it.moneyDecimal);
      if(max <= parseFloat(it.moneyDecimal)){
        max = parseFloat(it.moneyDecimal).toFixed(2);
      }
      if(min >= parseFloat(it.moneyDecimal)){
        min = parseFloat(it.moneyDecimal).toFixed(2);
      }
      sumQyzf += it.qyzf;
      sumFwf +=  it.serviceChargeMoney;
      sumSd += it.amount
  })
  item.maxDec = max;
  item.minDec = min;
  item.avgDec =  parseFloat(sumMoneyDecimal/item.billIds).toFixed(2);
  item.qyzf = sumQyzf;
  item.serviceChargeMoney = sumFwf;
  item.sumMoneyDecimal = sumMoneyDecimal;
  item.amount = sumSd;
  return [item];
}


const dataList = computed(() => {
  if (props.success) {
    return getSuccessList();
  }else{
    return props.list;
  }
})



</script>

<style>
</style>
